<?php
/* 用户收藏管理
* @Author: Jerry-chen
* @Date:   2014-07-15 13:51:46
* @Last Modified time: 2014-07-17 22:03:29
* @Last Modified by: Jerry-chen<uranusbill@163.com>
* @ Copyright (c) 2014 http://www.cooldreamer.com All rightsreserved.
*/

class CollectControl extends CommonControl
{
	public $model;
	/**
	 * [__auto 自动运行]
	 * @return [type] [description]
	 */
	public function __auto(){
		$this->model=K('Collect');
	}
	/**
	 * [index 收藏更表]
	 * @return [type] [description]
	 */
	public function index()
	{
		$this->display();
	}
	/**
	 * [collect 添加收藏]
	 * @return [type] [description]
	 */
	public function collect()
	{
		$goodsid = Q('get.goodsid');
		$uid = $_SESSION['uid'];
		// 先对她进行查询，看是否已经收藏 了此商品
		$oldgoodsid = $this->model->get_all(array('uid'=>$uid),array('goodsid')); 
		// p($oldgoodsid);die;
		if(is_array($oldgoodsid)){
			//循环比对
		foreach ($oldgoodsid as $k => $v) {
				//对拿出来的商品进行比较，如果存在，就提示有商品，不存在就插入
				if($goodsid == $v['goodsid']){
					$this->success('您已添加此商品','');
					}
			}
		}
		// 如果不存在就插入数据 
		// 插入数据
				$result = $this->model->add_(array('goodsid'=>$goodsid,'uid'=>$uid));
				if($result){
					$this->success('添加成功','',1);
				}else{
						$this->error('添加失败','',3);
				}
		}
			// p($oldgoodsid);die;r

}